package com.wenhuitec.sysui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.adapter.ApRadioBoxAdapter;

import java.util.List;
import java.util.Map;

public class ApRadioBoxDialog {
    private View view;
    private OnUserClick onUserClick;

    public void showApRadioBoxDialog(Context context, List<Map<String, String>> list) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        //2、设置布局
        view = View.inflate(context, R.layout.dialog_apl_radio, null);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();


        RecyclerView contentRv = dialog.findViewById(R.id.contentRv);
//        创建适配器，将数据传递给适配器
        ApRadioBoxAdapter mAdapter = new ApRadioBoxAdapter(context, list, (map -> {
            if (onUserClick != null) {
                onUserClick.onClick(map);
            }
            dialog.dismiss();
        }));
        //设置布局管理器
        contentRv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        //设置适配器adapter
        contentRv.setAdapter(mAdapter);

        //按钮隐藏
        TextView cancelBut = dialog.findViewById(R.id.cancelBut);
        cancelBut.setOnClickListener((v -> {
            dialog.dismiss();
        }));
    }

    public void setOnUserClick(OnUserClick onUserClick) {
        this.onUserClick = onUserClick;
    }

    public interface OnUserClick {
        void onClick(Map<String, String> map);
    }
}
